home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group98b.txt
/
000124_icon-group-sender _Wed Jul 8 14:01:48 1998.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Return-Path: <icon-group-sender>
Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
by baskerville.CS.Arizona.EDU (8.8.8/8.8.7) with SMTP id OAA02640
for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Wed, 8 Jul 1998 14:01:47 -0700 (MST)
Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
id AA10420; Wed, 8 Jul 1998 14:01:34 -0700
From: "Nevin :-] Liber" <nevin@pendragon-software.com>
To: <icon-group@optima.CS.Arizona.EDU>
Cc: "David Starner" <dstarner98@aasaa.ofe.org>
Subject: Re: Stripping excessive spaces
Date: Tue, 7 Jul 1998 19:55:41 -0500
Message-Id: <01bdaa0b$2191b000$c37546cf@NEVIN>
Mime-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-Msmail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.71.1712.3
X-Mimeole: Produced By Microsoft MimeOLE V4.71.1712.3
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
Content-Length: 2099
>I've just started programming in Icon, and I came across the problem of
>removing all the starting and ending spaces from a string. This was my
>solution, but I think it's terribly inefficent. Can some one help me
>with a better way to do this?
The built-in function trim() does the bulk of what you want to do. Check
out the documentation at
<http://www.cs.arizona.edu/icon/refernce/funcsz.htm#trim>.
If you are looking to express the algorithm as a one-liner, the following
should suffice:
procedure clip(s, c)
/c := ' '
s := string(s) | runerr(103, s)
c := cset(c) | runerr(104, c)
return reverse(trim(reverse(trim(s, c)), c))
end
If the "inefficiency" of reversing a string twice bothers you, you might
wish to write it as:
procedure clip(s, c)
/c := ' '
s := string(s) | runerr(103, s)
c := cset(c) | runerr(104, c)
trim(s, c) ? {
tab(many(c))
return tab(0)
}
end
These are both a little more general than what you asked for, as you can
clip any arbitrary cset (which defaults to space) instead of just spaces.
Also, this behaves a little differently than your algorithm does, as it
aborts with a runtime error instead of just failing if s is not convertible
to a string (and c is not null and not convertible to a cset).
If you wanted to generalize this a little more, say to use different csets
for the left and right sides (defaulting the left clip to space if none is
provided, and defaulting the right clip to be the same as the left clip if
non is provided), you might write it as:
procedure clip(s, cLeft, cRight)
/cLeft := ' '
/cRight := cLeft
s := string(s) | runerr(103, s)
cLeft := cset(cLeft) | runerr(104, cLeft)
cRight := cset(cRight) | runerr(104, cRight)
trim(s, cRight) {
tab(many(cLeft))
return tab(0)
}
__
Nevin ":-)" Liber <mailto:nevin@pendragon-software.com> (847) 816-9926
Senior Software Engineer, Pendragon Software <http://www.webfayre.com/>